<?php
/**
 * Created by PhpStorm.
 * User: Wu Ning
 * Date: 2018/5/20
 * Time: 16:55
 */
namespace App\Model\Backstage;
use App\Model\Base;
use Illuminate\Support\Facades\DB;
class RoomFollow extends Base
{
    public $table;
    public function __construct()
    {
        parent::__construct();
//        $this->table = 'follow_'.$this->branch_id;
        $this->table = 'follow';
    }

    /*
     * 添加房源跟进
     * Wu Ning
     */
    public function addRoomFollow($params,$customerId)
    {
             return DB::table($this->table)
            ->insertGetId([
                'f_room_id'=>$params['f_room_id'],
                'f_follow_up_way'=>$params['f_follow_up_way'],
                'f_user_id'=>$this->uid,
                'f_customers_id'=>$customerId ? $customerId : '',
                'f_content'=>$params['f_content'],
                'f_created_at' => time()
         ]);
    }


    /**
     * @param array $map
     * @param $pageLimit
     * @return mixed
     * 获取用户房源跟进数量排行
     * author:lxy
     */
    public function getRoomFollowNum($map = [],$pageLimit){
        $where = [
            'u_b_id' => $this->branch_id,
            'is_del' => 0,
            'u_status' => 1
        ];
        $user_list = DB::table('user')
            ->where($where)
            ->select(['u_id','u_username','u_type'])
            ->get()
            ->toArray();
        foreach($user_list as $k => $v){
            $num = DB::table($this->table)->where($map)->where('f_user_id', $v->u_id)->count();
            $v->num = $num;
        }
        $list = json_encode($user_list);
        $list = json_decode($list,true);
        array_multisort(array_column($list,'num'),SORT_DESC,$list);
        return $list;
    }

    /**
     * @param $roomInfoModel
     * @param $params
     * @return mixed
     * 移动员工房源更近
     */
    public function moves($roomInfoModel, $params){
        $room_ids = $roomInfoModel->moves($params, 2);
        return DB::table($this->table)
            ->whereIn('f_room_id', $room_ids)
            ->update([
                'f_user_id' => $params['to_uid']
            ]);
    }



}